package com.allen.service.impl;

import com.allen.bean.Dish;
import com.allen.bean.DishFlavor;
import com.allen.common.CustomException;
import com.allen.dao.DishDao;
import com.allen.dto.DishDto;
import com.allen.service.DishFlavorService;
import com.allen.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dragon1006xu@gmail.com
 * @create 2022/2/17 19:25
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Autowired
    DishFlavorService dishFlavorService;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //1.保存菜品信息到Dish表
        this.save(dishDto);
        //2.保存菜品口味信息到dish_flavor表
        //2.1 获取菜品口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //获取当前菜品id 并设置口味信息对应的菜品id
        Long dishId = dishDto.getId();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
        //2.2:保存菜品口味信息
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lambdaQueryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //1.修改菜品基本信息
        this.updateById(dishDto);
        //2.修改菜品的口味信息
        //获取菜品id
        Long dishId = dishDto.getId();
        //2.1 删除该菜品原有的口味信息
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dishId);
        dishFlavorService.remove(lambdaQueryWrapper);
        //2.2 新增该菜品的当前口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setId(null);
            flavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);
    }

    /**
     *删除菜品
     * @param ids
     */
    @Override
    public void removeByStatus(List<Long> ids) {
        //select count(*) from dish where id in(1,2,3) and status=1;
        //查询菜品状态，确认是否删除
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count = this.count(queryWrapper);
        if(count>0){
            //如果不能删除，抛出一个业务异常
            throw new CustomException("菜品正在售卖，不能删除");
        }
        //如果可以删除
        this.removeByIds(ids);
    }

    /**
     * 修改菜品状态
     * @param ids
     * @param status
     */
    @Override
    public void updateStatus(List<Long> ids, Integer status) {
        //根据status修改停售或启售状态
        //select * from dish where id in(1,2,3) and status=1;
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        List<Dish> dishList = this.list(queryWrapper);
        for (Dish dish : dishList) {
            if(dish.getStatus()!=status){
                dish.setStatus(status);
                log.info("-------------修改菜品状态----------");
                this.updateById(dish);
            }
        }
    }
}
